package com.compass.doctor.dialog

import android.app.Activity
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import com.compass.doctor.R
import com.compass.doctor.databinding.DialogAppendTagBinding
import com.compass.doctor.ui.patient.adapter.AppendTagAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.ext.onClick
import com.compass.framework.model.Tag
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.getStringFromResource
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.netease.yunxin.kit.common.utils.ScreenUtils

/**
 * Description： 新增标签弹窗
 * Date:2023/12/14 15:23
 */
class AppendTagDialog {

    class Builder(activity: FragmentActivity, private var patientId: String?) :
        BaseDialogFragment.Builder<Builder>(activity) {

        private val patientViewModel: PatientViewModel by activity.viewModels()
        private lateinit var appendTagAdapter: AppendTagAdapter
        private var patientTags: ArrayList<Tag> = ArrayList()
        private val mBinding: DialogAppendTagBinding =
            DialogAppendTagBinding.inflate(LayoutInflater.from(activity))
        init {
            initView()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
            gravity = Gravity.BOTTOM
            appendTagAdapter = AppendTagAdapter(1)
            mBinding.rcyTags.apply {
                val layoutFelManager = FlexboxLayoutManager(context)
                layoutFelManager.flexWrap = FlexWrap.WRAP // 按正常方向换行
                layoutFelManager.flexDirection = FlexDirection.ROW // 水平方向
                layoutFelManager.justifyContent = JustifyContent.FLEX_START // 左对齐
                layoutManager = layoutFelManager
                adapter = appendTagAdapter

            }
            appendTagAdapter.onItemClickListener = { _: View, position: Int ->
                patientTags[position].id?.let { patientViewModel.deletePatientTag(it) }
            }

            mBinding.tvCancel.onClick {
                dismiss()
            }
            mBinding.tvAdd.onClick {
                var tag = mBinding.edtInputContent.text.toString().trim()
                if (!TextUtils.isEmpty(tag)) {
                    if (patientTags.size<3){
                        patientViewModel.addPatientTag(patientId, tag)
                        return@onClick
//                        ScreenUtils.getDisplayWidth()
                    }else{
                        TipsToast.showTips("最多添加3个标签")
                        return@onClick
                    }

                }
                TipsToast.showTips(getStringFromResource(R.string.input_custom_tag))
            }


        }

         fun setTagsData(patientTags : ArrayList<Tag>, isAdd: Boolean) {
             this.patientTags = patientTags
             appendTagAdapter.setData(patientTags)
             if (isAdd) hideKeyboard()
//             dismiss()

        }


        /**
         * hide keyboard
         */
        private fun hideKeyboard() {
            val inputMethodManager= mBinding.edtInputContent.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager

            if (inputMethodManager.isActive){
                inputMethodManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS)
            }

        }

    }
}